 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ExamManagementSystem
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!chkInput())//检查输入合法性
                return;
            string loginType = cboLoginType.Text;
            bool loginResult = false;

            switch (loginType)
            {
                case "教师":
                    loginResult = TeacherLogin();
                    if (loginResult)
                    {
                        UserHelper.loginId = txtLoginId.Text.Trim();
                        UserHelper.loginType = loginType;
                        //此处显示教师窗口
                        this.Hide();
                        TeacherForm tf = new TeacherForm();
                        tf.Show();
                    }
                    break;
                case "管理员":
                    loginResult = AdminLogin();
                    if (loginResult)
                    {
                        UserHelper.loginId = txtLoginId.Text.Trim();
                        UserHelper.loginType = loginType;
                        //此处显示管理员窗口
                        this.Hide();
                        AdminForm af = new AdminForm();
                        af.Show();
                    }
                    break;
            }

            if(loginResult==false)
                MessageBox.Show("登录失败,请检查帐号或密码是否有误!", "试题管理系统(V1.0)", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        // 取消登录，退出应用程序
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 检查客户端的输入的合法性
        /// </summary>
        /// <returns>true：合法，false：不合法</returns>
        private bool chkInput()
        {
            string accountId = txtLoginId.Text.Trim();
            string pwd = txtLoginPwd.Text;

            if (accountId == "")
            {
                MessageBox.Show("帐号不允许为空!", "试题管理系统(V1.0)", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (pwd == "")
            {
                MessageBox.Show("密码不允许为空!", "试题管理系统(V1.0)", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            string loginType = cboLoginType.Text;

            if (loginType == "")
            {
                MessageBox.Show("请选择登录类型!", "试题管理系统(V1.0)", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 实现用户登录
        /// </summary>
        /// <param name="loginType"></param>
        /// <returns>true：登录成功，false：登录失败</returns>
        private bool UserLogin(string loginType)
        {
            string strsql = string.Format("select count(*) from {2} where LoginId='{0}' and PasWord='{1}'", txtLoginId.Text, txtLoginPwd.Text, loginType);
            SqlCommand comm = new SqlCommand(strsql, DBHelper.connection);
            try
            {
                DBHelper.connection.Open();
                int result = (int)comm.ExecuteScalar();
                return result > 0;
            }
            catch
            {
                return false;
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <returns></returns>
        private bool AdminLogin()
        {
            return this.UserLogin("myAdmin");
        }
        /// <summary>
        /// 教员登录
        /// </summary>
        private bool TeacherLogin()
        {
            return this.UserLogin("Teacher");
        }
    }
}
